<?php

namespace WMApi\Controller;

use Think\Controller;
use Think\Log;

use Com\Jdpay\JdpayNotify;
use SDXApi\Model\PaymentModel;
use WMApi\Service\OrderService;

/**
 * [外卖业务平台]京东支付回调
 * 
 * @author ^2_3^
 *
 */
class WmjdpaynotifyController extends Controller {
    
    /**
     * 京东支付异步判断支付结果
     * @author ^2_3^
     */
    public function jdpay_notifypay() {
        set_time_limit(0);
        
//         $this->write_log( 'jdpay begin: ' );
        $jdpayNotify = new JdpayNotify();
        $verify_result = $jdpayNotify->verifyNotify();
//         $this->write_log( 'jdpay data: '.json_encode($verify_result) );
        
        if( $verify_result ) {//验证成功
            $trade_result = $verify_result['result'];
            $out_trade_no = $verify_result['tradeNum'];//商户订单号;
            
            if( $verify_result['status'] == '2' && $trade_result['desc'] == 'success' ) {
                $out_trade_db = explode("s",$out_trade_no);
                $total_fee = $verify_result['amount']/100;
                
                /// 充值
                if( $out_trade_db[1] == PAY_SURPLUS ) {
                    $payment_m = new PaymentModel();
                    $flag_update = $payment_m->updata_recharg_log($out_trade_db[0]);    //更新支付状态;
                    
                    if( $flag_update ) {
                        $verify_result['out_trade_no'] = $out_trade_db[0];
                        $payment_m->update_jdpay_log( $verify_result, PAY_SURPLUS );
                        echo "success";
                        
                    }else {
                        echo "fail";
                    }
                    
                /// 外卖订单
                }else if( $out_trade_db[1] == PAY_WM_ORDER) {
                    /// 订单及支付信息
                    $order_service = new OrderService();
                    $order_pay_info = $order_service->get_pay_log( $out_trade_db[0], $out_trade_db[1] );
                    
                    /// 混合支付
                    if( floatval($total_fee)-floatval($order_pay_info["pay_fee"])<0 ){
                        $surplus['user_id'] = $order_pay_info['uid'];
                        $surplus['amount'] = $total_fee;
                        $surplus['process_type'] = 0;
                        $surplus['user_note'] = 0;
                        $surplus['payment'] = '京东支付';
                        $surplus['admin_note'] = '混合支付';
                        
                        $payment_m = new PaymentModel();
                        $p_log_id = $payment_m->add_recharge_log($surplus, $total_fee);
                        $flag_update = $payment_m->updata_recharg_log( $p_log_id );
                        
                        if( $flag_update ) {
                            $verify_result['out_trade_no'] = $p_log_id;
                            $payment_m->update_jdpay_log($verify_result, PAY_SURPLUS);
                            $order_service->tobalance_pay($out_trade_db[0]);
                            
                            echo "success";
                        }else {
                            echo "fail";
                        }
                    }
                    /// 全额支付
                    else {
                        $result_pay = $order_service->order_paid( $out_trade_db[0] );
                        
                        if( $result_pay['status'] ) {
                            $verify_result['out_trade_no'] = $out_trade_db[0];
                            $order_service->update_jdpay_log($verify_result, PAY_WM_ORDER);
                            
                            echo "success";
                        }else {
                            echo "fail";
                        }
                    }
                    
                }
                
            }
            
            //——请根据您的业务逻辑来编写程序（以上代码仅作参考）——
            //             echo "success";		//请不要修改或删除
            //
            
        }else {
            //验证失败
            echo "fail";
            //调试用，写文本函数记录程序运行情况是否正常
            //logResult("这里写入想要调试的代码变量值，或其他运行的结果记录");
        }
        
        
    }
    
    public function write_log( $message='', $filename='wm_jd_notifypay' ) {
        $log = new Log();
        $destination = C('LOG_PATH').date('y_m_d').'_'.$filename.'.log';
        
        $log->write($message, 'DEBUG', '', $destination);   
    }
    
}